package com.jsyl.lwbk.common.utils;

import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class AgeUtils {
    /**
     * 描述：根据年月日计算年龄,精确到日
     * 返回值：xx岁xx个月
     */
    public static String buildAge(String year, String month, String day) {
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.setTime(new Date());
        Integer yearBirth = nowCalendar.get(Calendar.YEAR);
        Integer monthBirth = 1;
        Integer dayOfMonthBirth = 1;
        if (StringUtils.hasText(year)) {
            yearBirth = Integer.parseInt(year);
        }
        if (StringUtils.hasText(month)) {
            month = month.replace("/","");
            month = month.replace(".","");
            month = month.replace("-","");
            monthBirth = Integer.parseInt(month);
        }
        if (StringUtils.hasText(day)) {
            dayOfMonthBirth = Integer.parseInt(day);
        }
        if (yearBirth < 0) {
            yearBirth = 0;
        }
        if (monthBirth < 0) {
            monthBirth = 0;
        }
        if (dayOfMonthBirth < 0) {
            dayOfMonthBirth = 0;
        }
        Calendar birthday = new GregorianCalendar(yearBirth, monthBirth, dayOfMonthBirth);
        Calendar now = Calendar.getInstance();
        int diffDay = now.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH);
        int diffMonth = now.get(Calendar.MONTH) + 1 - birthday.get(Calendar.MONTH);
        int diffYear = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
        //按照减法原理，先day相减，不够向month借；然后month相减，不够向year借；最后year相减。
        if (diffDay < 0) {
            diffMonth -= 1;
            //得到上一个月，用来得到上个月的天数。
            now.add(Calendar.MONTH, -1);
            diffDay = diffDay + now.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        if (diffMonth < 0) {
            diffMonth = (diffMonth + 12) % 12;
            diffYear--;
        }
        StringBuffer tag = new StringBuffer();
        if (diffYear > 0) {
            tag.append(diffYear + "岁");
        } else {
            tag.append("0岁");
        }
        if (diffMonth > 0) {
            tag.append(diffMonth + "个月");
        }
        if (diffYear == 0 && diffMonth == 0 && diffDay == 0) {
            tag.append("0岁");
        }
        return String.valueOf(tag);
    }

    public static String getGrade(Integer grade){
        String result="";
        if(grade ==null){
            return result;
        }
        switch (grade){
            case 0:result ="学前";break;
            case 1:result ="一年级";break;
            case 2:result ="二年级";break;
            case 3:result ="三年级";break;
            case 4:result ="四年级";break;
            case 5:result ="五年级";break;
            case 6:result ="六年级";break;
            case 7:result ="七年级";break;
            case 8:result ="八年级";break;
            case 9:result ="九年级";break;
            default:result="";break;
        }
        return result;
    }

    /**
     * 描述：根据年月日计算年龄,精确到月
     */
    public static int computationsAge(String birthYear, String birthMonth) {
        if (!StringUtils.hasText(birthYear)) {
            birthYear = "0";
        }
        if(!StringUtils.hasText(birthMonth)){
            birthMonth = "0";
        }
        // 得到当前时间的年、月、日
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int age = 0;
        try {
            int year = Integer.parseInt(birthYear);
            int month = Integer.parseInt(birthMonth);
            // 用当前年月日减去生日年月日
            int yearMinus = yearNow - year;
            int monthMinus = monthNow - month;

            int ageYear = yearMinus; // 先大致赋值
            if (yearMinus < 0) { // 选了未来的年份
                ageYear = 0;
            } else if (yearMinus == 0) { // 同年的，要么为1，要么为0
                if (monthMinus > 0) { // 选了未来的月份
                    ageYear = 0;
                }
            } else if (yearMinus > 0) {
                if (monthMinus >= 0) { // 同月份的，再根据日期计算年龄
                    ageYear = yearMinus;
                } else {
                    ageYear = yearMinus - 1;
                }
            }
            if (ageYear >= 0) {
                age = ageYear;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return age;
    }

}
